﻿@* --------------------------------------------------------------------------------------------------------------------
// <copyright file="MyFileUploadWidget.cshtml" company="Devbridge Group LLC">
// 
// Copyright (C) 2015,2016 Devbridge Group LLC
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 
// </copyright>
// 
// <summary>
// Better CMS is a publishing focused and developer friendly .NET open source CMS.
// 
// Website: https://www.bettercms.com 
// GitHub: https://github.com/devbridge/bettercms
// Email: info@bettercms.com
// </summary>
// -------------------------------------------------------------------------------------------------------------------- *@
@using Microsoft.Web.Mvc
@model BetterCms.Sandbox.Mvc4.Models.MyFileUploadViewModel


@using (Html.BeginForm("MyFileUploadWidget_Upload", "WidgetsController", FormMethod.Post, new { id = "upload_form", enctype = "multipart/form-data" }))
{
    <div>
        <label for="MyFile">MyFile:</label>
        <input type="file" name="MyFile" id="MyFile" />
        <input type="hidden" name="Name" value="test" />
    </div>
    @Html.SubmitButton("submit", "submit", new { id = "submit_button" });
}
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">

    $("#upload_form").submit(function (e) {
        e.preventDefault();

        var formData = new FormData();
        formData.append("Name", "anything");
        formData.append("MyFile", $('input[type=file]')[0].files[0]);
        $.ajax({
            url: "../Widgets/MyFileUploadWidget_Upload",
            type: "POST",
            data: formData,
            enctype: 'multipart/form_data',
            processData: false,
            contentType: false,
            success: function(r) {
                    alert(r);
                }
            }
        );
    });
</script>